// export function curry(fn: Function) {
//   const length = fn.length
//   const curryFn = (args: any) => (arg: any) => {
//     const curryArgs = args.concat(arg)
//     if (curryArgs.length === length) {
//       return fn(...curryArgs)
//     }
//     return curryFn(curryArgs)
//   }
//   return curryFn([])
// }

/**
 * 函数科里化
 * @param func
 * @example
 *
 * const sum = (a, b, c) => a * 100 + b * 10 + c * 1
 * const curriedSum = curry(sum)
 * sum(1, 2, 3) = 123 等价于
 * curriedSum()(1, 2, 3)
 * curriedSum(1, 2, 3)
 * curriedSum(1)(2, 3)
 * curriedSum(1, 2)(3)
 * curriedSum(1)(2)(3)
 */
export function curry(func: Function) {
  return function curried(...partialArgs: any[]) {
    if (partialArgs.length >= func.length) {
      return func.apply(this, partialArgs)
    } else {
      return function (...resArgs: any[]) {
        return curried.apply(this, partialArgs.concat(resArgs))
      }
    }
  }
}

/**
 * 函数科里化，从右往左
 * @param func
 * @example
 *
 * const sum = (a, b, c) => a * 100 + b * 10 + c * 1
 * const curriedSum = curryRight(sum)
 * sum(1, 2, 3) // 123
 * (curriedSum()(1, 2, 3) // 123
 * curriedSum(1, 2, 3) // 123
 * curriedSum(1)(2, 3) // 231
 * curriedSum(1,2)(3) // 312
 * curriedSum(1)(2)(3) // 321
 */
export function curryRight(func: Function) {
  return function curried(...partialArgs: any[]) {
    if (partialArgs.length >= func.length) {
      return func.apply(this, partialArgs)
    } else {
      return function (...resArgs: any[]) {
        return curried.apply(this, resArgs.concat(partialArgs))
      }
    }
  }
}
